package com.ow.dva.module.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ow.dva.module.account.entity.AccountInfo;
import com.ow.dva.module.account.entity.AccountPark;
import com.ow.dva.module.account.service.AccountInfoService;
import com.ow.dva.module.account.service.AccountParkService;
import com.ow.dva.module.base.controller.BaseController;
import com.ow.dva.module.base.entity.json.RT;
import com.ow.dva.module.base.entity.param.DvaPage;
import com.ow.dva.module.city.entity.City;
import com.ow.dva.module.city.service.CityService;
import com.ow.dva.module.park.entity.Park;
import com.ow.dva.module.park.entity.Parkinglot;
import com.ow.dva.module.park.service.ParkService;
import com.ow.dva.module.park.service.ParkinglotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


/**
 * <p>
 * 停车场表 前端控制器
 * </p>
 *
 * @author YanZengBao
 * @version V1.0
 * @since 2020-02-19
 */
@Controller
@Api(tags = "停车场表 相关控制器")
@RequestMapping("/park")
public class ParkController extends BaseController {

    /**
     * 分页获取停车场表数据列表信息
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "分页获取", notes = "批量获取停车场表信息列表，分页，可携带条件")
    @RequiresPermissions("park:page")
    @GetMapping(value = "/page")
    public String page(DvaPage<Park> dvaPage, @RequestParam Map<String, Object> param, Model model) {

        DvaPage<Park> page = parkService.page(dvaPage, param);
        model.addAttribute("page", page);
        model.addAttribute("param", param);
        return "park/list";
    }

    /**
     * 描述： 跳转场内支付二维码页面
     * 创建时间：2020/4/6  YanZengBao
     * 修改时间：2020/4/6  YanZengBao 加注释
     * 修改时间：
     */
    @RequestMapping(value = "/enterParkingInfo")
    public String enterParkingInfo(String parkId, ModelMap modelMap) {
        Park park = parkService.getById(parkId);
        modelMap.put("parkId", parkId);
        modelMap.put("terminaId", park.getTerminaCode());
        return "park/enterParkingInfo";
    }

    /**
     * 进入停车场表添加页面。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入添加页面", notes = "进入停车场表添加页面")
    @RequiresPermissions("park:add")
    @GetMapping(value = "/add")
    public String add(Model model) {
        List<City> cityList = cityService.selectProvince();
        model.addAttribute("cityList", cityList);
        return "park/add";
    }

    /**
     * 添加停车场表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "添加", notes = "添加单个停车场表信息")
    @RequiresPermissions("park:add")
    @PostMapping("/save")
    @ResponseBody
    public RT save(Park park) {
        //验证停车场编号是否存在
        Long count = parkService.countByParkNumber(park.getNumber());
        if (count > 0) return RT.error(1, "停车场编号已存在！");

        park.setCreateTime(new Date());
        park.setParkinglotSurplus(park.getParkinglotTotal());

        City province = cityService.getById(park.getProvince());
        City city = cityService.getById(park.getCity());
        City county = cityService.getById(park.getCounty());
        if (province != null) park.setProvince(province.getName());
        if (city != null) park.setCity(city.getName());
        if (county != null) park.setCounty(county.getName());

        return parkService.save(park) ? RT.ok("保存成功！") : RT.error(1, "保存失败！");
    }

    /**
     * 进入停车场表编辑页面。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入编辑页面", notes = "进入停车场表编辑页面")
    @RequiresPermissions("park:edit")
    @GetMapping(value = "/edit")
    public String edit(String id, Model model) {

        model.addAttribute("data", parkService.getById(id));

        return "park/edit";
    }

    /**
     * 修改停车场表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "修改", notes = "修改单个停车场表信息")
    @RequiresPermissions("park:edit")
    @PostMapping("/update")
    @ResponseBody
    public RT update(Park park) {

        if (park.getParkinglotTotal() != null && park.getParkinglotSurplus() != null) {
            if (park.getParkinglotTotal() < park.getParkinglotSurplus()) return RT.error(1, "剩余车位数不能大于总车位数！");
            int count = parkinglotService.count(new QueryWrapper<Parkinglot>().eq("park_id", park.getId()));
            if (park.getParkinglotTotal() < count) return RT.error(1, "车位总数不能小于现有车位数！");
        }
        return parkService.updateById(park) ? RT.ok("修改成功！") : RT.error(1, "修改失败！");
    }

    /**
     * 删除单个停车场表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "删除", notes = "删除单个停车场表信息")
    @RequiresPermissions("park:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RT delete(@ApiParam(name = "要删除的停车场表ID") String id) {

        return parkService.removeById(id) ? RT.ok() : RT.error(1);
    }

    /**
     * 批量删除多个停车场表信息。
     * 创建时间：2020-02-19 YanZengBao
     * 修改时间：2020-02-19 YanZengBao 加注释
     * 修改时间：
     */
    @ApiOperation(value = "批量删除", notes = "批量删除多个停车场表信息")
    @RequiresPermissions("park:delete")
    @PostMapping("/deleteByIds")
    @ResponseBody
    public RT deleteByIds(@ApiParam(name = "要删除的停车场表ID List") @RequestParam List<String> ids) {

        return parkService.removeByIds(ids) ? RT.ok() : RT.error(1);
    }


    /**
     * 描述： 跳转全部停车场列表
     * 创建时间：2020/5/12  YanZengBao
     * 修改时间：2020/5/12  YanZengBao 加注释
     * 修改时间：
     */
    @GetMapping(value = "/parkList")
    public String parkList(String parkManagerId,Model model){
        List<Park> parkList = parkService.getParkList();
        model.addAttribute("parkManagerId",parkManagerId);
        model.addAttribute("parkList",parkList);
        return "accountInfo/parkList";
    }


    /**
     * 描述：检索停车场
     * 创建时间：2020/5/12  YanZengBao
     * 修改时间：2020/5/12  YanZengBao 加注释
     * 修改时间：
     */
    @RequestMapping(value = "/chooseParkByParm")
    @ResponseBody
    public RT chooseParkByParm(@RequestParam Map<String, Object> param){

        //处理参数空格
        String name = (String)param.get("name");
        if(name != null){
            param.replace("name",name,name.trim());
        }
        List<Park> parkList = parkService.list(param);
        return RT.ok(parkList);
    }

    /**
     * 描述： 查看车场管理员管辖的停车场列表详情
     * 创建时间：2020/5/12  YanZengBao
     * 修改时间：2020/5/12  YanZengBao 加注释
     * 修改时间：
     */
    @GetMapping(value = "/parkInfoList")
    public String parkInfoList(String parkManagerId,Model model){
        List<Park> parkList = new ArrayList<>();
        AccountInfo accountInfo = accountInfoService.getById(parkManagerId);
        if(accountInfo != null){
            List<AccountPark> accountParks = accountParkService.getByAccountId(accountInfo.getAccountId());
            if(accountParks != null && !accountParks.isEmpty()){
                for (AccountPark ap:accountParks) {
                    Park park = parkService.getById(ap.getParkId());
                    if(park != null){
                        parkList.add(park);
                    }
                }
            }
        }
        model.addAttribute("parkList",parkList);
        model.addAttribute("parkManagerId",parkManagerId);
        return "accountInfo/parkInfoList";
    }

    /**
     * 描述： 管理员解绑某个停车场
     * 创建时间：2020/5/12  YanZengBao
     * 修改时间：2020/5/12  YanZengBao 加注释
     * 修改时间：
     */
    @PostMapping("/untyingPark")
    @ResponseBody
    public RT untyingPark(String id,String parkManagerId){
        AccountInfo accountInfo = accountInfoService.getById(parkManagerId);
        if(accountInfo == null) return RT.error(1,"解绑失败,管理员信息有误！");
        return accountParkService.untyingPark(new AccountPark(accountInfo.getAccountId(),id)) ? RT.ok("解绑成功！") : RT.error(1,"解绑失败！");
    }


    /***
     * 点击获取城市名称
     * @param cityName 城市名称
     * @return 点击成功
     */
    @GetMapping(value = "/chooseAddress")
    public String chooseAddress(String cityName,String longitude,String latitude, Model model) {
        model.addAttribute("cityName", cityName);
        model.addAttribute("latitude", latitude);
        model.addAttribute("longitude", longitude);
        return "park/map";
    }

    @GetMapping(value = "/parkRelated")
    public String parkRelated(Model model) {
        List<Park> parkList = parkService.list();
        model.addAttribute("parkList", parkList);
        return "park/parkRelated";
    }

    @RequestMapping(value = "/parkToGet")
    @ResponseBody
    public RT parkToGet(String id) {
        Park park = parkService.getById(id);
        return RT.ok(park);
    }

    @RequestMapping(value = "/berthByParkId")
    @ResponseBody
    public List<Map> berthByParkId(String parkId) {

        List<Map> mapList = new ArrayList<>();

        Map map = new HashMap();
        map.put("berthNum", 0);//总车位数
        map.put("berthSurplus", 0);//剩余车位数
        map.put("occupyNum", 0);//占用车位数
        mapList.add(map);
        Integer berthNum = 0;
        Integer berthSurplus = 0;
        Integer occupyNum = 0;

        if (parkId.equals("") || parkId == null) {
            List<Park> parks = parkService.getParkList();
            for (Park p : parks) {
                berthNum = berthNum + p.getParkinglotTotal();
                berthSurplus = berthSurplus + p.getParkinglotSurplus();
                occupyNum = berthNum - berthSurplus;
                mapList.get(0).put("berthNum", berthNum);
                mapList.get(0).put("berthSurplus", berthSurplus);
                mapList.get(0).put("occupyNum", occupyNum);
            }
        } else {
            Park park = parkService.getById(parkId);
            if (park != null) {
                berthNum = park.getParkinglotTotal();
                berthSurplus = park.getParkinglotSurplus();
                occupyNum = berthNum - berthSurplus;
                mapList.get(0).put("berthNum", berthNum);
                mapList.get(0).put("berthSurplus", berthSurplus);
                mapList.get(0).put("occupyNum", occupyNum);
            }
        }
        return mapList;
    }

    @Resource
    ParkService parkService;
    @Resource
    CityService cityService;
    @Resource
    ParkinglotService parkinglotService;
    @Resource
    private AccountInfoService accountInfoService;
    @Resource
    private AccountParkService accountParkService;
}
